/*
 * facerec.cpp
 *
 *  Created on: Sep 14, 2012
 *      Author: heinstein
 */

/*
 * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>.
 * Released to public domain under terms of the BSD Simplified license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the organization nor the names of its contributors
 *     may be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 *   See <http://www.opensource.org/licenses/bsd-license>
 */

#include "facerec.hpp";

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string.h>


static void load_pgm(vector<Mat>& images, vector<int>& labels) {

	string workDir = "/home/heinstein/faces/";

	DIR *dir ,*dir2;
	struct dirent *ent ,*ent2;
	dir = opendir (workDir.c_str());
	if (dir != NULL) {

		/* print all the files and directories within directory */
		while ((ent = readdir (dir)) != NULL) {
			if ( ent->d_type == DT_DIR && ent->d_name[0] == 's')
			{

				stringstream st;
				st << workDir << ent->d_name;
				dir2 = opendir(st.str().c_str());
				while ((ent2 = readdir (dir2)) != NULL)
				{

					if(ent2->d_type == DT_REG && string(ent2->d_name).substr(string(ent2->d_name).size() -4, 4) == ".pgm" )
					{
						stringstream st2;
						st2 << st.str() << "/" << ent2->d_name;

						stringstream st3;
						st3 << ent->d_name;
						string st4 = st3.str();
						st4.erase(0,1);

						cout << st2.str() << endl << st4.c_str() << endl;

						Mat test = imread(st2.str(), 0);


						images.push_back(test);

						labels.push_back(atoi(st4.c_str()));

					}
				}
			}
		}
		closedir (dir);
	} else {
		/* could not open directory */
		perror ("");
		//		return EXIT_FAILURE;
	}

}

void init_facerec(Ptr<FaceRecognizer> model) {
	// These vectors hold the images and corresponding labels.
	vector<Mat> images;
	vector<int> labels;
	// Read in the data. This can fail if no valid
	// input filename is given.
	try {
		load_pgm(images, labels);
	} catch (cv::Exception& e) {
		cerr << "Error opening file \"" << "\". Reason: " << e.msg << endl;
		// nothing more we can do
		exit(1);
	}

	// Quit if there are not enough images for this demo.
	if(images.size() <= 1) {
		string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
		CV_Error(CV_StsError, error_message);
	}

	model->train(images, labels);

}


